// pages/articles/detail/index.js
import wxRequest from '../../../utils/wxRequest.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showReplyInput: false,
    news_id: '', // 帖子id
    reply_content: '', // 评论或回复内容
    datalist: {},
    commentArray: [],
    lid:'',//被回复者的id
    nickName:"",
    currentName:'',//存储该帖子作者的昵称
    page: 1,
    numbers: 15,
    recommend:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showNavigationBarLoading();
      if (!app.globalData.userInfo) return
        this.setData({ news_id: options.id, userId: app.globalData.userInfo.user_id }); 
       wxRequest.getRequest('/m/news/detail', { id: options.id }).then(res => {
         this.setData({
           datalist: res.data,
           currentName: res.data.nickname,
           nickName: res.data.nickname
         });console.log(res.data)
         wx.hideNavigationBarLoading();
       }).catch(error => {
         console.log(error, 'error');
         wx.hideNavigationBarLoading();
       });
       wxRequest.getRequest('/m/news/index',{page:1,number:1}).then(res => {
         this.setData({ recommend: res.data.newsItem });
       }).catch(error => {
         console.log(error, 'error');
       });
       // 初始化详情信息
       this.onMessage();
   
  },
  // 获取留言列表、点击获取更多评论
  onMessage(cb){ 
    const obj = { news_id: this.data.news_id, page: this.data.page, number: this.data.numbers};
    wxRequest.getRequest('/m/comment/index', obj).then(res => {
      const totalComment = this.data.commentArray.concat(res.data.commentItem);
      this.setData({ 
        commentArray: totalComment, 
         page: this.data.page+1
         })
      wx.hideNavigationBarLoading();
      return cb();
    }).catch(error => {
      console.log(error, 'error');
      wx.hideNavigationBarLoading();
    })
    
  },
  // 点赞
  onZan:function(e){
    wxRequest.getRequest('/m/comment/zan', {news_id:this.data.news_id}).then(res => {
     if(res.code===0){
      this.showToast('点赞成功！','success',2000);
     }
      else if(res.code===1){
        this.showToast('你已经点过赞了！','fail',2000);
      }
    }).catch(error => {
      this.showToast('点赞失败！', 'fail', 2000);
    });
  
  },
  showToast(str, types, dur) {
    wx.showToast({
      title: str,
      icon: types,
      duration: dur
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },
  // 页面滚动到评论区 
  handleScrollToComment() {
    var query = wx.createSelectorQuery()
    query.select('.comment-list').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (res) {
      // #the-id节点的上边界坐标
      // res[1].scrollTop // 显示区域的竖直滚动位置
      wx.pageScrollTo({
        scrollTop: res[0].top,
        duration: 300
      })
    })
  },

  // 显示弹出评论input （写留言）
  handleShowReplyInput(e) {
    this.setData({
      showReplyInput: true
    })
  },

  //发送留言 input 失去焦点
  handleReplyInputBlur(e) {
    var val = e.detail.value.trim();
    if (val){
      this.setData({
        reply_content:val, showReplyInput: false
      });
      const obj = {
        news_id: this.data.news_id,
        text: this.data.reply_content,
        p_user_id: this.data.uid,
        pid: this.data.lid
      };
      wxRequest.getRequest('/m/comment/add', obj).then(res => {
        wx.showToast({
          title: '评论成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({ reply_content: '', page: 1, commentArray:[]});
        this.onMessage(() => {
          this.handleScrollToComment();
        });
        
      }).catch(error => {
        console.log(error, 'error');
      })
    }
      this.setData({
        showReplyInput: false, nickName:this.data.currentName
      })

   
  },
  // 评论input框 input事件
  handleReplyBindInput(e) {
   
  },


  // 点击评论内容调起回复框 (回复评论)
  handleReplyComment(e) {
    const uid = e.currentTarget.dataset.uid ;console.log(uid)
    if(uid==this.data.userId){
      return false;
    }
    else{
      this.setData({ lid: e.currentTarget.dataset.lid, nickName: e.currentTarget.dataset.name, uid: uid });
      this.handleShowReplyInput();
    }
    // 此处应有评论id 
  },

  // 跳转详情页面
  onDetailTap: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './index?id=' + id
    })
  },

  // 点击评论下的回复调起回复框
  handleReplyReply(e) {
     
  },
  onShare(e){
    this.onShareAppMessage();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (from) { 
    console.log(from)
  }
})